﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Useful.GUI
{
    public class ToolStripDateTime : ToolStripControlHost
    {
        public event EventHandler ValueChanged;

        private DateTimePicker dtpValue = null;

        public ToolStripDateTime() : base(new DateTimePicker())
        {
            dtpValue = (DateTimePicker)Control;
        }

        public DateTimePicker DateTimePickerControl
        {
            get
            {
                return Control as DateTimePicker;
            }
        }

        public bool ShowCheckBox
        {
            get
            {
                return dtpValue.ShowCheckBox;
            }
            set
            {
                dtpValue.ShowCheckBox = value;
            }
        }

        public DateTime? Value
        {
            get
            {
                return dtpValue.Checked ? (DateTime?)dtpValue.Value : null;
            }
            set
            {
                if (value != null)
                {
                    dtpValue.Value = value.Value;
                    dtpValue.Checked = true;
                }
                else
                {
                    dtpValue.Checked = false;
                }
            }
        }

        public DateTime? Date
        {
            get
            {
                return dtpValue.Checked ? (DateTime?)dtpValue.Value.Date : null;
            }
        }

        public DateTimePickerFormat Format
        {
            get
            {
                return dtpValue.Format;
            }
            set
            {
                dtpValue.Format = value;
            }
        }

        public string CustomFormat
        {
            get
            {
                return dtpValue.CustomFormat;
            }
            set
            {
                dtpValue.CustomFormat = value;
            }
        }

        protected override void OnSubscribeControlEvents(Control control)
        {
            base.OnSubscribeControlEvents(control);

            DateTimePicker odtp = (DateTimePicker)control;

            odtp.ValueChanged += new System.EventHandler(odtp_ValueChanged);
        }

        protected override void OnUnsubscribeControlEvents(Control control)
        {
            base.OnUnsubscribeControlEvents(control);

            DateTimePicker odtp = (DateTimePicker)control;

            odtp.ValueChanged -= new System.EventHandler(odtp_ValueChanged);
        }

        private void odtp_ValueChanged(object sender, System.EventArgs e)
        {
            if (ValueChanged != null)
                ValueChanged(this, e);
        }
    }
}